package utils;

import Address.Address;
import bean.*;
import server.*;

import java.security.KeyPair;
import java.util.List;
import java.util.Scanner;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:该类用于对三个需求的实现进行封装
 * @DateTime:2021/12/21 10:09
 **/
public class Select {

    //该方法用于对实现聚合函数功能进行封装
    public static void selectJuhe(){
        //创建键盘输入对象
        Scanner scanner = new Scanner(System.in);
        //声明变量用于实现while循环
        boolean sum = true;

        System.out.println("\n=======欢迎使用聚合数据查询系统=======");
        while (sum){
            System.out.println("您可以选择：\n0、退到上一级\n1、查询热门视频\n2、查询天气\n3、查询号码归属地\n4、查寻星座运势");
            System.out.print("请输入想使用的功能序号：");
            String i = scanner.next();

            switch (i){
                case "0":
                    System.out.println("欢迎您下次再来！\n");
                    sum = false;
                    break;
                case "1":
                    List<HotVideo> hotVideo = HotVideoService.getHotVideo();
                    System.out.println("\n查询到了"+hotVideo.size()+"条数据");
                    for (HotVideo o : hotVideo){
                        System.out.println(o.getTitle());
                        System.out.println(o.getShare_url());
                    }
                    break;
                case "2":
                    System.out.print("请输入所想查询的城市：");
                    String city = scanner.next();
                    try {
                        List<WeatherRealtime> weather = Weather.getWeather(city);
                        System.out.println("\n"+city+"今日天气："+weather.get(0).getInfo()+"，温度："+weather.get(0).getTemperature()+"度");
                    }catch (Exception e){
                        System.out.println("\n查询错误：请输入正确内容\n");
                    }
                    break;
                case "3":
                    System.out.print("请输入所想查询的号码：");
                    String phone = scanner.next();
                    try {
                        List<PhoneResult> phone_result = Phone.getPhone(phone);
                        System.out.println("\n手机号："+ phone + "的用户归属地为：" + phone_result.get(0).getProvince()+phone_result.get(0).getCity());
                    }catch (Exception e){
                        System.out.println("\n查询错误：请输入正确内容\n");
                    }
                    break;
                case "4":
                    System.out.print("请输入想要查询的星座：");
                    String condName = scanner.next();
                    Cons_Today cons = Cons.getCons(condName);
                    if (cons.getName()!=null){
                        PrintTodayCons.printTodayCons(cons);
                    }else {
                        System.out.println("\n查询错误：请输入正确内容\n");
                    }
                    break;
                default:
                    System.out.println("暂不支持该功能，请重新选择");
            }
        }
    }

    //该函数用于对实现RPC功能进行封装
    public static void selectRpc(){
        Scanner scanner = new Scanner(System.in);
        //定义布尔类型变脸实现while无限循环
        Boolean sum = true;
        System.out.println("\n=======欢迎使用botcoin系统=======");
        //while无限循环，只有用户输入0时，循环条件变成false，结束循环
        while (sum){
            System.out.println();
            System.out.println("您可以选择：\n0.退到上一级" +
                    "\n1.getBestBlockHash(获取当前节点最高位的区块哈希值)" +
                    "\n2.getBlockCount(获取当前节点区块高度)" +
                    "\n3.getBlockHash(查找指定高度区块的哈希值)" +
                    "\n4.getBlock(通过哈希值获取指定区块具体信息)" +
                    "\n5.getDifficulty(获取出块难度)");
            System.out.print("请输入选项：");
            String i = scanner.next();

            //根据用户输入实现响应功能
            switch (i){
                case "0":
                    System.out.println("退出成功，欢迎下次使用!!!\n");
                    sum=false;
                    break;
                case "1":
                    //获取当前节点最高位的区块哈希值
                    RpcResult bestBlockHash = GetBestBlockHash.getBestBlockHash(1001);
                    System.out.println("当前节点最高位的区块哈希值为："+bestBlockHash.getResult());
                    break;
                case "2":
                    //获取当前节点区块高度
                    RpcResult blockCount = GetBlockCount.getBlockCount(1002);
                    System.out.println("当前节点区块高度："+blockCount.getResult());
                    break;
                case "3":
                    //查找指定高度区块的哈希值
                    System.out.println("请输入需要查找的区块高度(不可超过当前节点高度”31390“)");
                    String count = scanner.next();
                    try {
                        int height = Integer.parseInt(count);
                        RpcResult blockHash = GetBlockHash.getBlockHash(1003, height);
                        System.out.println("高度"+count+"的区块哈希值为："+blockHash.getResult());
                    }catch (Exception e){
                        System.out.println("\n查询错误：请输入正确内容\n");
                    }
                    break;
                case "4":
                    //通过哈希值获取指定区块具体信息
                    System.out.println("请输入需要查找的区块哈希值");
                    String hash = scanner.next();
                    try {
                        RpcGetBlock block = GetBlock.getBlock(1004, hash);
                        utils.PrintGetBlock.printGetBlock(block);
                    }catch (Exception e){
                        System.out.println("\n查询错误：请输入正确内容\n");
                    }
                    break;
                case "5":
                    //获取出块难度
                    RpcResult difficulty = GetDifficulty.getDifficulty(1004);
                    System.out.println("当前块难度为："+difficulty.getResult());
                    break;
                default:
                    System.out.println("暂不支持该功能，请重新输入选项");
            }
        }
    }

    //该函数用于对生成非压缩型公钥的比特币地址功能进行封装
    public static void selectAddress(){
        Scanner scanner = new Scanner(System.in);
        //定义布尔类型变脸实现while无限循环
        Boolean sum = true;
        System.out.println("\n=======欢迎使用比特币生成地址系统=======");
        //while无限循环，只有用户输入0时，循环条件变成false，结束循环
        while (sum){
            System.out.println();
            System.out.println("您可以选择：\n0.退到上一级" +
                    "\n1.生成非压缩型公钥比特币地址" +
                    "\n2.验证比特币地址是否合法");
            System.out.print("请输入选项：");
            String i = scanner.next();
            Address address = new Address();
            switch (i){
                case "1":
                    KeyPair keyPair = address.generateKey();
                    String addr = address.newAddress(keyPair);
                    System.out.println("生成到的比特币地址为："+addr);
                    break;
                case "2":
                    System.out.print("请输入需要验证的比特币地址：");
                    String next = scanner.next();
                    boolean isVali = address.validateAddr(next);
                    if (isVali){
                        System.out.println("该比特币地址是合法的");
                    }else {
                        System.out.println("该比特币地址是非法的");
                    }
                    break;
                case "0":
                    System.out.println("欢迎您再次使用\n");
                    sum=false;
                    break;
                    default:
                        System.out.println("暂不支持该选项，请重新输入选项");
            }

        }
    }

}

